package com.basswold.nasaesa.client;

import java.util.ArrayList;

import com.basswold.nasaesa.client.ConstructionModule.CardinalDirection;
import com.basswold.nasaesa.client.ConstructionModule.ModuleType;

public class CrossBase extends BaseConstructor {
	
	FoodControlWingFactory foodControlWing1;
	FoodControlWingFactory foodControlWing2;
	DormWingFactory dormWing1;
	DormWingFactory dormWing2;

	public CrossBase(final ModuleList modList) {
		super(modList);
	}

	@Override
	public Module[][] BuildBase() {
		
		/*
		 * Construct a graph of the base
		 */
		ConstructionConnectorModule centralConnector = 
			(ConstructionConnectorModule)modules.getModuleOfType(ModuleType.PLAIN);
		ConstructionConnectorModule westConnector = 
			(ConstructionConnectorModule)modules.getModuleOfType(ModuleType.PLAIN);
		ConstructionConnectorModule eastConnector = 
			(ConstructionConnectorModule)modules.getModuleOfType(ModuleType.PLAIN);
		
		centralConnector.setWestConnection(westConnector);
		centralConnector.setEastConnection(eastConnector);
		westConnector.setEastConnection(centralConnector);
		eastConnector.setWestConnection(centralConnector);
		
		// Construct the needed building wings.
		this.buildWings();
		
		// Attach completed wings to base structure.
		centralConnector.setNorthConnection(dormWing1.finishConstruction());
		centralConnector.setSouthConnection(foodControlWing2.finishConstruction());
		westConnector.setWestConnection(foodControlWing1.finishConstruction());
		eastConnector.setEastConnection(dormWing2.finishConstruction());
		
		// Convert graph of base to 2D array
		return convertToOutputModules(trimArray(convertToArray(centralConnector)));
	}

	@Override
	public ArrayList<BuildingWingFactory> getWingFactories() {
		// This will store the separate wings as they are constructed.
		ArrayList<BuildingWingFactory> wings = 
			new ArrayList<BuildingWingFactory>();
		
		// Create wing factories that will construct separate wings
		foodControlWing1 = new FoodControlWingFactory(modules);
		foodControlWing2 = new FoodControlWingFactory(modules);
		dormWing1 = new DormWingFactory(modules);
		dormWing2 = new DormWingFactory(modules);
		
		foodControlWing1.initialize(CardinalDirection.EAST, true);
		foodControlWing2.initialize(CardinalDirection.NORTH, true);
		dormWing1.initialize(CardinalDirection.SOUTH, true);
		dormWing2.initialize(CardinalDirection.WEST, true);
		
		wings.add(foodControlWing1);
		wings.add(dormWing1);
		wings.add(foodControlWing2);
		wings.add(dormWing2);
		
		return wings;
	}
}
